package Q19

class Main {

}

fun main() {

}

class ListNode(var `val`: Int) {
    var next: ListNode? = null
}

//删除链表的倒数第N个结点
class Solution {
    fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {
        var n = n
        if (head == null) {
            return null
        }
        var listnode = ListNode(0)
        listnode.next = head
        var fast : ListNode? = listnode
        var slow : ListNode? = listnode

        while(n > 0){
            n--
            fast = fast!!.next
        }

        var res : ListNode? = null
        while(fast != null){
            res = slow
            slow = slow!!.next
            fast = fast.next
        }

        res!!.next = slow!!.next
        slow.next = null
        return listnode.next
    }
}

